package com.hsource.doctorcenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.hsource.common.utils.CommonResp;
import com.hsource.common.utils.resultUtils.controller.BaseController;
import com.hsource.doctorcenter.domain.doctor.Doctor;
import com.hsource.doctorcenter.service.doctor.IDoctorService;
import com.hsource.doctorcenter.util.ServletUtils;
import com.hsource.doctorcenter.util.StringUtils;
import com.hsource.doctorcenter.util.http.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/10/23 14:38
 * @describe
 */
@Slf4j
@Controller
@CrossOrigin
public class HealthController extends BaseController {
    @Autowired
    private IDoctorService doctorService;

    private final static String character="&";


    /**
     * 糖尿病
     * @return
     */
    @PostMapping("/diabetes")
    @ResponseBody
    public CommonResp diabetes(){
        Map<String,String> map=new HashMap<>();
        HttpServletRequest request = ServletUtils.getRequest();
        Map<String, String> stringStringMap = convertRequestParamsToMap(request);
        final String[] paramsValue = {""};
        stringStringMap.forEach((k,v)->{
            paramsValue[0] = paramsValue[0] + k+"="+v;
            if(StringUtils.isBlank(paramsValue[0])) {
            }else{
                paramsValue[0]=paramsValue[0]+character;
            }
        });

        String s = OkHttpUtil.postFormParamsTokenTest("http://tnb.market.alicloudapi.com/alicloudapi/chronic/diabetes?"+paramsValue[0], map,"APPCODE 94404993dd5b4151a59dbff83d5ed35a");
        JSONObject jsonObject = JSONObject.parseObject(s);
        if(jsonObject.getString("code").equals("0000")){
            Object data = jsonObject.get("data");
            return success(data);
        }else{
            return failed(jsonObject.getString("msg"));
        }
    }

    /**
     * 血压
     * @param highValue
     * @param lowValue
     * @param disease
     * @return
     */
    @PostMapping("/elasticity")
    @ResponseBody
    public CommonResp elasticity(String highValue,String lowValue,String disease ){
        Map<String,String> map=new HashMap<>();
        String paramsValue="highValue="+highValue+"&lowValue="+lowValue+"&disease="+disease;
        String s = OkHttpUtil.postFormParamsTokenTest("http://dtbp.market.alicloudapi.com/alicloudapi/dailyTest/bloodPressure?"+paramsValue, map,"APPCODE 94404993dd5b4151a59dbff83d5ed35a");
        JSONObject jsonObject = JSONObject.parseObject(s);
        if(jsonObject.getString("code").equals("0000")){
            Object data = jsonObject.get("data");
            return success(data);
         }else{
            return failed(jsonObject.getString("msg"));
        }

    }

    /**
     * 解析请求中参数
     * @param request
     * @return
     */
    private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();

        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();

        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            log.info("支付宝=="+name+"&值==="+values[0]);
            int valLen = values.length;

            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }

        return retMap;
    }

}

